/*
 * Copyright 2007-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.paoding.rose.web.var;

import java.util.ArrayList;

import javax.servlet.http.Cookie;

import net.paoding.rose.Rc;

import org.apache.commons.lang.StringUtils;

/**
 * 绑定某一次请求和响应的Cookie帮助类
 * 
 * @see Rc#cookie()
 * 
 * @author qieqie.wang@paoding.net
 * 
 */
public class CookieHelper {

	/**
	 * 增加一个Cookie
	 * 
	 * @param name
	 * @param value
	 * @return
	 */
	public CookieHelper add(String name, String value) {
		Rc.response().addCookie(new Cookie(name, value));
		return this;
	}

	/**
	 * 增加一个Cookie
	 * 
	 * @param name
	 * @param value
	 * @param domain
	 *            null或空串表示忽略
	 * @param expiry
	 *            null或<0表示忽略
	 */
	public CookieHelper add(String name, String value, String domain, Integer expiry) {
		Cookie cookie = new Cookie(name, value);
		if (expiry != null && expiry > 0)
			cookie.setMaxAge(expiry);
		if (StringUtils.isNotBlank(domain)) {
			cookie.setDomain(domain);
		}
		Rc.response().addCookie(cookie);
		return this;
	}

	/**
	 * 增加一个Cookie
	 * 
	 * @param cookie
	 */
	public CookieHelper add(Cookie cookie) {
		Rc.response().addCookie(cookie);
		return this;
	}

	/**
	 * 删除Cookie
	 * 
	 * @param cookie
	 */
	public CookieHelper removeCookie(String cookieName) {
		Cookie[] cookies = find(cookieName);
		for (Cookie cookie : cookies) {
			remove(cookie);
		}
		return this;
	}

	/**
	 * 删除Cookie
	 * 
	 * @param cookie
	 * @return
	 */
	public CookieHelper remove(Cookie cookie) {
		Cookie toRemove = new Cookie(cookie.getName(), null);
		toRemove.setMaxAge(0);
		toRemove.setPath(cookie.getPath());
		Rc.response().addCookie(toRemove);
		return this;
	}

	/**
	 * 返回指定名称的Cookie对象，然后通过Cookie对象获取更详细的信息
	 * 
	 * @param name
	 * @return
	 */
	public Cookie get(String name) {
		Cookie[] cookies = find(name);
		return cookies.length == 0 ? null : cookies[0];
	}

	/**
	 * 返回指定名称的Cookie数组对象，然后通过Cookie对象获取更详细的信息
	 * 
	 * @param name
	 * @return
	 */
	public Cookie[] find(String name) {
		Cookie[] cookies = getAllCookies();
		ArrayList<Cookie> values = new ArrayList<Cookie>(2);
		for (Cookie cookie : cookies) {
			if (cookie.getName().equals(name)) {
				values.add(cookie);
			}
		}
		return values.toArray(new Cookie[values.size()]);
	}

	private static Cookie[] EMPTY_COOKIE = new Cookie[0];

	private Cookie[] getAllCookies() {
		Cookie[] cookies = Rc.request().getCookies();
		if (cookies == null) {
			return EMPTY_COOKIE;
		}
		return cookies;
	}
}
